package com.nulldev.util.manage;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.JVM.reflections.Reflections;

public class nullUtil {
	public static final int NULLUTIL_GENERATION = 3;
	public static final float VERSION = 3.02f;
	/**
	 * Backport, features from newer JRE's on older JRE's. <br>
	 * Disabled Features: <br>
	 * <ul>
	 * <li>Legacy Reflections API</li>
	 * </ul>
	 */
	public static final float BACKPORT_VERSION = 0.04f;
	public static final String CODENAME = "Hydra";
	public static final String VERSION_STRING = "3.0.2";
	public static final Version LIBRARY_VERSION = new Version(VERSION_STRING);

	public static final long BUILD = 371;
	public static final boolean IS_NON_MAINLINE_BUILD = true;
	public static final boolean IS_LTS = false;
	public static final boolean IS_SERVICE_PACK = false;
	public static final boolean IS_ANDROID_BUILD = false;

	public static final boolean DISABLE_ALL_SILENT_EXCEPTIONS = false;

	public static final int MINIMAL_JAVA_VERSION = 8;
	public static final int MAXIMUM_JAVA_VERSION = 16;
	public static final int[] SUPPORTED_JAVA_VERSIONS =
		{
				/* Pre JRE 8 is NO longer supported, use 2.x instead. */
				8, 9, 10, 11, /* Full support */
				12, 13, 14, 15, 16 /* Partial support (forwards compatible) */
				/* JRE 17 and newer is NOT supported due to changes in the Reflections API */
		};

	private static final boolean BYPASS_JVM_SUPPORT_CHECK = true;

	public static boolean isSupportedJVM() {
		if (BYPASS_JVM_SUPPORT_CHECK && JVM.version() >= 17f) {
			JVM.println("[!] WARNING: This JVM is not officially supported by nullUtil.");
			JVM.println("    You will NOT receive support in any way. Continuing.");
			return true;
		}
//		if (JVM.version() < 1.8f || JVM.version() >= 17) {
//			JVM.println("============== nullUtil 3.x ==============");
//			JVM.println("Sorry for interrupting.");
//			JVM.println();
//			JVM.println("Sadly your JVM is not supported.");
//			JVM.println("Either this is your fault or Oracle's.");
//			JVM.println();
//			JVM.println("Exiting.");
//			JVM.println("ERROR_CODE: NU_INVALID_JVM_VER");
//			JVM.println("==========================================");
//			FastIO.get().flush();
//			JVM.exit(-1);
//			return false;
//		}else
		return true;
	}

	public static final int NULLUTIL_VERSION_STRING_FORMAT_VERSION_ONE = 1;
	public static final int NULLUTIL_VERSION_STRING_FORMAT_VERSION_TWO = 2;
	public static final int NULLUTIL_VERSION_STRING_FORMAT_VERSION_THREE = 3;

	public static String getVersionString() {
		return getVersionString(NULLUTIL_VERSION_STRING_FORMAT_VERSION_THREE);
	}

	public static String getVersionString(final boolean useNewVersion) {
		return getVersionString(useNewVersion ? NULLUTIL_VERSION_STRING_FORMAT_VERSION_TWO : NULLUTIL_VERSION_STRING_FORMAT_VERSION_ONE);
	}

	public static String getVersionString(final int formatVersion) {
		switch (formatVersion) {
			case NULLUTIL_VERSION_STRING_FORMAT_VERSION_THREE: {
				final StringBuilder sb = new StringBuilder();
				sb.append((IS_ANDROID_BUILD ? "nullUtil Mobile " : "nullUtil "));
				sb.append(VERSION_STRING);
				sb.append(" (build ");
				sb.append(BUILD);
				if (IS_NON_MAINLINE_BUILD) {
					sb.append(", development");
				}
				if (IS_LTS) {
					sb.append(", LTS");
				}
				if (IS_SERVICE_PACK) {
					sb.append(", SP");
				}
				sb.append(")");
				__append_components(sb);
				return sb.toString();
			}
			case NULLUTIL_VERSION_STRING_FORMAT_VERSION_TWO:
				return (IS_ANDROID_BUILD ? "nullUtil Mobile " : "nullUtil ") + VERSION_STRING + " (build " + BUILD
						+ (IS_NON_MAINLINE_BUILD ? ", development" : "") + (IS_LTS ? ", LTS" : "") + (IS_SERVICE_PACK ? ", SP" : "") + ")";
			case NULLUTIL_VERSION_STRING_FORMAT_VERSION_ONE:
				return (IS_ANDROID_BUILD ? "nullUtil Mobile " : "nullUtil ") + VERSION_STRING + "-" + BUILD + (IS_NON_MAINLINE_BUILD ? "-dev" : "")
						+ (IS_LTS ? "-lts" : "") + (IS_SERVICE_PACK ? "-sp" : "");
			default:
				throw new UnsupportedOperationException("Unknown format version: " + formatVersion);
		}
	}

	private static String __components_cache = null;

	private static void __append_components(final StringBuilder _sb) {
		if (__components_cache != null) {
			_sb.append(__components_cache);
		} else {
			final StringBuilder sb = new StringBuilder();
			if (Reflections.is("com.nulldev.util.android.virtual.os.Build"))
				sb.append(" +virtual_android");
			if (Reflections.is("com.nulldev.util.internal.externals.okhttp.OkHttpClient"))
				sb.append(" +okhttp");
			if (Reflections.is("com.nulldev.util.internal.externals.luminis.nu_httpclient.KwikHttpRequest"))
				sb.append(" +kwik");
			if (Reflections.is("com.nulldev.util.internal.externals.bouncycastle.tls.DefaultTlsClient"))
				sb.append(" +bouncycastle");

			__components_cache = sb.toString();
			_sb.append(__components_cache);
		}
	}
}
